<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品货号" prop="itemNo">
        <el-input
          v-model="queryParams.itemNo"
          placeholder="请输入商品货号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="商品颜色" prop="color">
        <el-input
          v-model="queryParams.color"
          placeholder="请输入商品颜色"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品尺码" prop="size">
        <el-input
          v-model="queryParams.size"
          placeholder="请输入商品尺码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="是否已入系统" prop="intoSystem">
        <el-input
          v-model="queryParams.intoSystem"
          placeholder="请输入是否已入系统"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    <!--  <el-form-item label="入系统时间" prop="intoSystemTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.intoSystemTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择入系统时间">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item label="是否是买断款" prop="byoutOrnot">
        <el-input
          v-model="queryParams.byoutOrnot"
          placeholder="请输入是否是买断款"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="售货员" prop="sellBy">
        <el-input
          v-model="queryParams.sellBy"
          placeholder="请输入售货员"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="售卖时间" prop="sellTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.sellTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="默认查询当日">
        </el-date-picker>
      </el-form-item>
     <!-- <el-form-item label="店铺代码" prop="shopId">
        <el-input
          v-model="queryParams.shopId"
          placeholder="请输入店铺代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bill:detail:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bill:detail:edit']"
        >修改</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleIntoSystem"
          v-hasPermi="['bill:detail:remove']"
        >批量入系统</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleOutSystem"
          v-hasPermi="['bill:detail:remove']"
        >批量取消入系统</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bill:detail:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bill:detail:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" v-if="false"/>
      <el-table-column label="日结id" align="center" prop="dailySettlementId" v-if="false"/>
      <el-table-column label="订单号" align="center" prop="orderNo" width="130"/>
      <el-table-column label="商品货号" align="center" prop="itemNo" width="160"/>
      <el-table-column label="状态" align="center"   prop="intoSystem" :formatter="intoSystemFormate" />
      <el-table-column label="买断款?" align="center" prop="byoutOrnot" :formatter="byOutFormate"></el-table-column>
      <el-table-column label="商品颜色" align="center" prop="color" />
      <el-table-column label="商品尺码" align="center" prop="size"/>
      <el-table-column label="销售价钱" align="center" prop="priceDefined" />
      <el-table-column label="当日系统价钱" align="center" prop="priceSystem" />
      <el-table-column label="入系统时间" align="center" prop="intoSystemTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.intoSystemTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="买断款状态" align="center" prop="byoutStatus" :formatter="byoutStatusFormate" />
      <el-table-column label="买断款出库时间" align="center" prop="byoutDeliveryTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.byoutDeliveryTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="售货员" align="center" prop="sellByName" />
      <el-table-column label="销售时间" align="center" prop="sellTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sellTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="店铺代码" align="center" prop="shopId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleIntoSystem(scope.row)"
            v-hasPermi="['bill:detail:intoSystem']"
          >入系统</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleOutSystem(scope.row)"
            v-hasPermi="['bill:detail:intoSystem']"
          >取消入系统</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bill:detail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bill:detail:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改当日销售管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" style="color: #C03639">
        <el-form-item label="商品货号" prop="itemNo">
          <el-input v-model="form.itemNo" placeholder="[英文模式下]使用扫码枪扫码商品条码" style="color: #C03639" />
        </el-form-item>

        <el-form-item label="商品颜色" prop="color">
          <el-input v-model="form.color" placeholder="货号输入为[DW22C1111AB10235]类型时,颜色可不填" />
        </el-form-item>
        <el-form-item label="商品尺码" prop="size">
          <el-input v-model="form.size" placeholder="货号输入为[DW22C1111AB10235]类型时,尺码可不填" />
        </el-form-item>
        <el-form-item label="销售价钱" prop="priceDefined">
          <el-input v-model="form.priceDefined" placeholder="请输入销售价钱,必填" />
        </el-form-item>
        <el-form-item label="当日系统价钱" prop="priceSystem">
          <el-input v-model="form.priceSystem" placeholder="请输入当日系统价钱 可不填" />
        </el-form-item>
          <div>
              <el-form-item label="是否买断款" prop="byoutOrnot">
                <el-radio-group v-model="form.byoutOrnot" size="medium">
                  <el-radio v-for="(item, index) in byoutOrnotOptions" :key="index" :label="item.value"
                            :disabled="item.disabled" border>{{item.label}}</el-radio>
                </el-radio-group>
              </el-form-item>
          </div>

     <!--   <el-form-item label="售货员" prop="sellBy">
          <el-input v-model="form.sellBy" placeholder="请输入售货员" />
        </el-form-item>-->

        <el-form-item label="售货员">
          <el-select v-model="form.sellBy"  placeholder="请选择">
            <el-option
              v-for="item in userInfosOptions"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDetail, getDetail, delDetail, addDetail, updateDetail ,exportDetail,intoSystem ,outSystem,getUserInfo} from "@/api/dusto/bill/detail/detail";

export default {
  name: "Detail",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 当日销售管理表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //用户信息
      userInfosOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dailySettlementId: null,
        itemNo: null,
        orderNo: null,
        color: null,
        size: null,
        priceDefined: null,
        priceSystem: null,
        intoSystem: null,
        intoSystemTime: null,
        byoutOrnot: null,
        byoutStatus: null,
        byoutDeliveryTime: null,
        sellBy: null,
        sellTime: null,
        shopId: null
      },
      // 表单参数
      form: {
        byoutOrnot: '买断款',
      },
      //买断款1 未买断0 对应
      byoutOrnotOptions: [{
        "label": "非买断",
        "value": 0
      }, {
        "label": "已买断",
        "value": 1
      }],
      // 表单校验
      rules: {
        itemNo:[{required:true,message:"商品型号不能为空",trigger:"blur"}],
        byoutOrnot: [{
          required: true,
          message: '是否买断款不能为空',
          trigger: 'change'
        }],
        priceDefined: [
          { required: true, message: "当前售价不能为空", trigger: "blur" }
        ],
        sellBy: [
          { required: true, message: "售货员信息不能为空", trigger: "blur" }
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询当日销售管理列表 */
    getList() {
      this.loading = true;
      listDetail(this.queryParams).then(response => {
        this.detailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dailySettlementId: null,
        itemNo: null,
        orderNo: null,
        color: null,
        size: null,
        priceDefined: null,
        priceSystem: null,
        intoSystem: null,
        intoSystemTime: null,
        byoutOrnot: null,
        byoutStatus: null,
        byoutDeliveryTime: null,
        sellBy: null,
        sellTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        shopId: null,
        userInfoList: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    //买断款1 未买断0 对应
    byOutFormate(row,index){
      if (row.byoutOrnot == 1 ) {
        return "已买断";
      } else if(row.byoutOrnot == 0 || row.byoutOrnot == null){
        return "未买断";
      }
    },
    //是否入系统
    intoSystemFormate(row,index){
      if (row.intoSystem == 1 ) {
        return "已入系统";
      } else if(row.intoSystem == 0 ){
        return "未入系统";
      }
    },
    //买断款状态0 = 未出库  1 = 已出库
    byoutStatusFormate(row,index){
      if (row.byoutStatus == 1 ) {
        return "已出库";
      } else if(row.byoutStatus == 0 ){
        return "在库中！";
      } else if(row.byoutStatus == null){
        return "非买断款";
      }
    }
    ,
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getUserInfo().then(response => {
        this.userInfosOptions = response.userInfoList;
        this.open = true;
      });
     // this.open = true;
      this.title = "添加当日销售管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改当日销售管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /**入系统按钮操作*/
    handleIntoSystem(row){
      const ids = row.id || this.ids;
      this.$confirm('是否确认将编号为"' + ids + '"的商品入系统?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return intoSystem(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("成功更改为已入系统");
      })

    },

    /**取消入系统按钮操作*/
    handleOutSystem(row){
      const ids = row.id || this.ids;
      this.$confirm('是否确认将编号为"' + ids + '"的商品取消入系统?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return outSystem(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("成功更改为已入系统");
      })

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除当日销售管理编号为"' + ids + '"的数据项？').then(function() {
        return delDetail(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bill/detail/export', {
        ...this.queryParams
      }, `detail_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
